import threading
import time


# def run(n):
#     print('task',n)
#     time.sleep(2)
#     print('task done',n,threading.current_thread(),threading.active_count())
# start_time = time.time()
# listt = []#存线程实例
# for i in range(50):
#     t = threading.Thread(target=run,args=('T->%s'%i,))
#     t.start()
#     listt.append(t)#为了不阻塞后面线程的启动，不在这里join，先放到一个列表里
# for t in listt:#循环线程实例列表，等待所有线程执行完毕
#     t.join()
# print('all the threads finished',threading.current_thread(),threading.active_count())
# print('cost:',time.time()-start_time)


def run(n):
    print('task',n)
    time.sleep(2)
    print('task done',n)
start_time = time.time()
listt = []#存线程实例
for i in range(50):
    t = threading.Thread(target=run,args=('T->%s'%i,))
    t.setDaemon(True)#将当前线程设置为守护线程，就不用写循环join
    t.start()
    listt.append(t)#为了不阻塞后面线程的启动，不在这里join，先放到一个列表里
# for t in listt:#循环线程实例列表，等待所有线程执行完毕
#     t.join()
print('all the threads finished',threading.current_thread(),threading.active_count())
print('cost:',time.time()-start_time)